package p2p.logic.connection.communication.gateway;

import java.util.Set;
import java.util.UUID;

import p2p.logic.connection.Connection;
import p2p.logic.connection.ConnectionOwner;
import p2p.logic.connection.client.Client;
import p2p.logic.connection.communication.message.Message;
import p2p.logic.connection.communication.type.Listen;
import p2p.logic.log.Logger;

public abstract class Gateway {
	
	protected ConnectionOwner owner;
	
	private int port;
	private String ip;
	
	protected Connection listen;
	
	public Gateway(String ip, int port, ConnectionOwner owner) {
		super();
		this.owner = owner;
		this.port = port;
		this.ip = ip;
	}

	
	public abstract void send(Client client, Message message);

	public abstract void send(Set<Client> clients);
	
	public abstract void send(Client client, UUID uuid);
	
	public void waitForAck(UUID uuid) {
		
		Logger.debug(this.owner.getId() + " is waiting for ack " + uuid.toString());
		
	}
	
	public Connection listen(int port) {
		
		this.listen = new Connection(new Listen(this.owner.getCommunicationHandler(), port),true);
		
		new Thread(listen,"Listen").start();
		
		return listen;
	}

	public void stop(){
		this.listen.stop();		
	}
	
	public Gateway getOwner() {
		return owner.getGateway();
	}
	
	public int getPort(){
		return this.port;
	}
	
		
	public String getIp(){
		return this.ip;
	}
	
	
}
